<?php
class SaveCategory extends Action {
	public function execute($params) {
		check_login ( ADMIN_LOGIN_PAGE );
		$category = rqsts ( array ('cid' => 0,'title','name','note','display' => 1,'up_cid' => 0,'up_name','url_path','url_pattern','bind','type'), true );
		
		$cateModel = new WebPageCategory ();
		if (empty ( $category ['cid'] )) { // 新增
			unset ( $category ['cid'] );
		}
		if(empty($category['url_pattern'])){
			$category['url_pattern'] = '{cp}/{Y}/{m}/{d}/{id}.html';
		}
		if(rqset('home') && !empty($category['bind'])){
			$category['home'] = 1;
		}else{
			$category['home'] = 0;
		}
		// 验证用户数据的合法性
		$valid = $this->validate ( 'category', $category, $cateModel );
		if ($valid === true) { // 用户数据合法
			$rst = $cateModel->saveCategory ( $category );
			if (! empty ( $rst )) {
				if (! rqset ( 'goon' )) {
					sess_add ( 'page_tip_info', '<strong>恭喜,</strong>频道栏目信息已经保存完成.' );
					$category ['cid'] = $rst ['cid'];
					sess_add ( 'category_info', $category );
					Response::redirect ( './?Ctlr=EditCategory&suc=1&cid=' . $rst ['cid'] );
				} else {
					Response::redirect ( './?Ctlr=AddCategory' );
				}
			} else {
				sess_add ( 'category_info', $category );
				sess_add ( 'page_tip_info', '<strong>出错啦!</strong>' . $cateModel->last_error_msg () );
				Response::redirect ( unkeepargs($this->referer,array('up_cid','up_cname')) );
			}
		}
		sess_add ( 'category_error', $valid );
		sess_add ( 'category_info', $category );
		Response::redirect ( unkeepargs($this->referer,array('up_cid','up_cname')));
	}
}